package cn.com.dhcc.turbo.service.registry;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.dao.CrudDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.NiceException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.service.AbstractCrudService;
import cn.com.dhcc.turbo.dao.registry.DomainInfoDao;
import cn.com.dhcc.turbo.entity.registry.DomainInfo;
import cn.com.dhcc.turbo.entity.registry.SynchronizeInfo.SyncOperType;
import cn.com.dhcc.turbo.entity.registry.SynchronizeInfo.SyncType;
import cn.com.dhcc.turbo.service.registry.sync.SynchronizeHandler;

/**
 * 业务域管理service
 * 
 * @日期：2014-5-15 下午4:15:44
 * @作者：张洪涛
 * @版权所有：东华软件
 * @版本：1.0
 */
@Service
public class DomainInfoService extends AbstractCrudService<DomainInfo> {
	@Autowired
	private DomainInfoDao domainDao;
	@Autowired
	NodeInfoService nodeInfoService;
	@Autowired
	SynchronizeHandler synchronizeHandler;
	/**
	 * 获取节点业务域
	 * @param pager
	 * @param condition
	 * @return
	 * @throws ServiceException
	 */
	public PagerData<DomainInfo> list(Pager pager, WhereCondition condition)
			throws ServiceException {
		try {
			return domainDao.list(pager, condition);
		} catch (DaoException e) {
			throw new ServiceException("获取业务域列表失败", e);
		}
	}
	/**
	 * 获取业务域
	 * @param nodeId
	 * @return
	 * @throws ServiceException
	 */
	public List<DomainInfo> getDomainByNodeId(String nodeId) throws ServiceException {
		try {
			return domainDao.getDomainByNodeId(nodeId);
		} catch (DaoException e) {
			throw new ServiceException("获取业务域列表失败", e);
		}
	}
	/**
	 * 添加业务域
	 * @param vo
	 * @throws ServiceException
	 * @throws NiceException 
	 */
	public void addAndSync(DomainInfo vo)throws ServiceException, NiceException {
		try {
			domainDao.insert(vo);
			synchronizeHandler.syncToAllNodesFromLocalNode( SyncType.DOMAIN_INFO, false, vo, SyncOperType.UPDATE,true);
		} catch (DaoException e) {
			throw new ServiceException("添加业务域失败", e);
		} catch (NiceException e) {
			throw e;
		}
	}
	/**
	 * 获取业务域
	 * @param id
	 * @return
	 * @throws ServiceException
	 */
	public DomainInfo get(String id) throws ServiceException {
		try {
			return domainDao.get(id);
		} catch (DaoException e) {
			throw new ServiceException("获取业务域失败", e);
		}
	}
	/**
	 * 修改业务域
	 * @param vo
	 * @throws ServiceException
	 * @throws NiceException 
	 */
	public void updateAndSyncDomain(DomainInfo vo) throws ServiceException, NiceException {
		try {
			super.update(vo);
			synchronizeHandler.syncToAllNodesFromLocalNode( SyncType.DOMAIN_INFO, false, vo, SyncOperType.UPDATE,true);
		} catch (ServiceException e) {
			throw new ServiceException("修改业务域失败", e);
		} catch (NiceException e) {
			throw e;
		}
		
	}
	
	@Override
	public CrudDao<DomainInfo> getCrudDao() {
		return domainDao;
	}
	/**
	 * 获取本节点业务域
	 * @return
	 * @throws DaoException
	 */
	public List<DomainInfo> getLocalDomain() throws ServiceException {
		try {
			return domainDao.getLocalDomain();
		} catch (DaoException e) {
			throw new ServiceException("获取本节点业务域失败", e);
		}
	}
	/**
	 * 获取节点业务域信息
	 * @param nodeId
	 * @return
	 * @throws DaoException
	 */
	public List<DomainInfo> getAll() throws ServiceException {
		try {
			return domainDao.getAll();
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}
		/**
	 * 验证属性唯一性
	 * @param property　属性
	 * @param newValue	新值
	 * @param oldValue　旧值
	 * @return
	 * @throws ServiceException
	 */
	public boolean isPropertyUnique(String property, String newValue, String oldValue)
			throws ServiceException {
		try {
			return domainDao.isPropertyUnique(property, newValue, oldValue);
		} catch (DaoException e) {
			throw new ServiceException("验证属性唯一性失败", e);
		}
	}
		public String MaxCode() throws ServiceException {
			try {
				return domainDao.MaxCode();
			} catch (DaoException e) {
				throw new ServiceException("业务域编码失败", e);
			}
		}
}
